//
//  BGGoodInfoViewController.m
//  bigMall
//
//  Created by LTOVE on 16/8/2.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGGoodInfoViewController.h"

#import "BGDateResult.h"

#import "BGGoodInfoDataResult.h"

#import "BGGoodInfoFramDataModel.h"

#import "BGShopAddListViewController.h"

#import "BGMessageContentFrameModel.h"

#import "BGGetAddListDataModel.h"
#import "BGGoodInfoResult.h"
#import "BGAddCollectModel.h"
#import "LTOVEDynamicCreakButton.h"
#import "BGCheckCollect.h"
#import "BGLoginViewController.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"
#import "BGGetImageInfoModel.h"
#import "BGCheckReportModel.h"
@interface BGGoodInfoViewController (){

    NSArray *_addListArray;//立即下单数据源数组
    NSString *_num;
    NSString *_leastNum;
    NSArray *_collBtnArray;
}

@end

@implementation BGGoodInfoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [BGDateResult checkCollectWithType:@"goods" objId:self.goodId success:^(NSObject *result) {
        BGCheckCollect *model = (BGCheckCollect *)result;
        NSLog(@"%@",model.status_str);
        self.cancelID = model.idStr;
        if ([model.status_str isEqualToString:@"收藏"]) {
            self.isRect = NO;
            [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
        }else if([model.status_str isEqualToString:@"取消收藏"]){
            self.isRect = YES;
            [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
        }else
            return ;
    } failure:^(NSError *error) {
        
        
    }];
    
    // Do any additional setup after loading the view.
}
#pragma mark - 加载数据 BGCheckReportModel
- (void)setGoodId:(NSString *)goodId
{
    _goodId = goodId;
    [MBProgressHUD showMessage:nil];
    [BGDateResult loadGoodInfoWithGoodsId:goodId success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGBasicresModel *itemModel = (BGBasicresModel *)result;
        BGGoodInfoResult *resModel = [BGGoodInfoResult objectWithKeyValues:itemModel.res];
        [MBProgressHUD showMessage:nil];
        
        [BGDateResult getImageInfoWithIds:resModel.check_report success:^(NSObject *result) {
            [MBProgressHUD hideHUD];
            BGGetImageInfoModel *imageModel = (BGGetImageInfoModel *)result;
            NSMutableArray *array = [NSMutableArray array];
            for (BGGetImageInfoModelRes *modelss in imageModel.res) {
                BGCheckReportModel *MyImageModel = [BGCheckReportModel new];
                MyImageModel.url = modelss.url;
                [array addObject:MyImageModel];
                
            }
            
            resModel.check_reports = array;
            
            NSArray *arr = [BGGoodInfoFramDataModel getDataArrayWithBGGoodInfoResult:resModel];
            self.dataArray = arr;
            BGGoodInfoResult *mmm = resModel;
//            BGGoodInfoResult *mmm = (BGGoodInfoResult *)result;
            _num = mmm.number;
            _leastNum = mmm.least_number;
            //        [arr writeToFile:@"/Users/LTOVE/Desktop/123xc.plist" atomically:YES];
            //        GWLog(@"%@",arr);
            [self.tableView reloadData];
//            _addListArray = [BGGetAddListDataModel getDataArrayWithBGGoodInfoResult:(BGGoodInfoResult *)result];
            _addListArray = [BGGetAddListDataModel getDataArrayWithBGGoodInfoResult:resModel];
            
        } failure:^(NSError *error) {
            
            
        }];
        
        
        
        
       

    } failure:^(NSError *error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)dynamicButtonClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
            GWLog(@"点击了收藏");
            [self collectBtnClick];
            break;
        case 1:
            GWLog(@"点击了下单");
            [self pushToShopAddListController];
            break;
            
        default:
            break;
    }
}

- (void)collectBtnClick
{
    if (!self.isRect) {
        [self addCollection];
    }else
        [BGDateResult checkCollectWithType:@"goods" objId:self.goodId success:^(NSObject *result) {
            BGCheckCollect *model = (BGCheckCollect *)result;
            NSLog(@"%@",model.status_str);
            self.cancelID = model.idStr;
            [self cancelCollection];
            
        } failure:^(NSError *error) {
            
            
        }];

}

- (void)cancelCollection
{
    [BGDateResult cancelCollectWithId:self.cancelID success:^(NSObject *result) {
        BGAddCollectModel *model = (BGAddCollectModel *)result;
        NSLog(@"%@",model.status);
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:model.msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            if ([model.status isEqualToString:@"1"]) {
                self.isRect = NO;
                [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
                
            }
            //            self.btnArray = @[@"已收藏",@"立即下单"];
        }];
        
        [alertView addAction:cancel];
        
        [self presentViewController:alertView animated:YES completion:^{
            
            
        }];
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)addCollection
{
    [BGDateResult addCollectWithType:@"goods" objId:self.goodId success:^(NSObject *result) {
        BGAddCollectModel *model = (BGAddCollectModel *)result;
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:model.msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if ([model.status isEqualToString:@"1"] && !self.isRect) {
                self.isRect = YES;
                [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
            }else if ([model.status isEqualToString:@"1"] && self.isRect){
                self.isRect = NO;
                [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
            }else if([model.status isEqualToString:@"-2"]){
                BGLoginViewController *vc = [[BGLoginViewController alloc]init];
                
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([model.status isEqualToString:@"-3"] && [model.msg isEqualToString:@"您已经收藏过"]){
                self.isRect = YES;
                [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
            }else
                return ;
            
            //            self.btnArray = @[@"已收藏",@"立即下单"];
        }];
        
        [alertView addAction:cancel];
        
        [self presentViewController:alertView animated:YES completion:^{
            
            
        }];
        
    } failure:^(NSError *error) {
        
        
    }];

}

//- (void)alertViewWith
#pragma mark -
#pragma mark - 跳转下单页面
- (void)pushToShopAddListController
{
    NSArray *ara = _addListArray;
//    GWLog(@"%@",ara);
    BGShopAddListViewController *vc = [BGShopAddListViewController initAddListviewWithDataArray:ara];
    vc.goodId = self.goodId;
//    NSLog(@"%@",self.goodId);
    vc.Num = _num;
    vc.leastNum = _leastNum;
    vc.navigationItem.title = @"立即下单";
    vc.hidesBottomBarWhenPushed = YES;
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"";
    self.navigationItem.backBarButtonItem = back;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)viewWillAppear:(BOOL)animated
{
//    [BGDateResult checkCollectWithType:@"goods" objId:self.goodId success:^(NSObject *result) {
//        BGCheckCollect *model = (BGCheckCollect *)result;
//        if ([model.status_str isEqualToString:@"收藏"]) {
//            self.isRect = YES;
//            self.btnArray = @[@"收藏",@"立即下单"];
//        }else{
//            self.isRect = NO;
//            self.btnArray = @[@"取消收藏",@"立即下单"];
//        }
//    } failure:^(NSError *error) {
//        
//        
//    }];
    self.btnArray = @[@"收藏",@"立即下单"];
    self.isRect = NO;
    NSLog(@"%@",_collBtnArray);
//    self.btnArray = _collBtnArray;
    [super viewWillAppear:animated];
}


@end
